<?php
	require_once('includes/common.inc.php');
	
	$id = $_GET['id'];
	$action = $_GET['action']; 
	
	$dbProvider = new UserDataProvider();
	
	$newRate = $dbProvider->getNewRating($id);
	$oldRate = $newRate;
	$lastAction = $dbProvider->getLastAction( $_SESSION['email'], $id );

	if( ($action == -1) && ($lastAction != '-') )
	{
		$newRate = $newRate - 1;
		$action = '-';
	}
	else
	{ 
		if( ($action == 1) && ($lastAction != '+') )
		{
			$newRate = $newRate + 1;
			$action = '+';
		}	
	}
	
	$dbProvider->updateRate($newRate, $id);
	
	if($newRate != $oldRate)
	{
		$dbProvider->updateUserAction($_SESSION['email'], $id, $action);
	}

	echo $newRate;
?>